/*   This file is part of Super Luigi Bros.
 *
 *   Super Luigi Bros is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Super Luigi Bros is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Super Luigi Bros.  If not, see <http://www.gnu.org/licenses/>.
 */
package luigibros.data.xml.level;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import luigibros.data.xml.level.ImageMap.SpriteRecord;

//TODO encapsulation!  
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Manifest")
public class Level {
	@XmlAccessorType(XmlAccessType.FIELD)
	@XmlType
	public static class OccupentRecord {
		@XmlElement(required = true, nillable = false, name = "class_name")
		private String className;
		@XmlElement(required = true, nillable = false, name = "pos_x")
		private double posX;
		@XmlElement(required = true, nillable = false, name = "pos_y")
		private double posY;
		@XmlElement(required = true, nillable = true, name = "creation_string")
		private String creationString;
		
		public void setClassName(String className){this.className = className;}
		public void setClassName(Class<?> _class) {className = _class.getName();}
		public void setPosX(double posX) {this.posX = posX;}
		public void setPosY(double posY) {this.posY = posY;}
		public void setPos(double posX, double posY) {this.posX = posX; this.posY = posY;}
		public void setCreationString(String string) {creationString = string;}
		
		public String getClassName() {return className;}
		public double getPosX() {return posX;}
		public double getPosY() {return posY;}
		public String getCreationString() {return creationString;}
	}
	@XmlElement(required = true, nillable = false, name = "occupent")
	public OccupentRecord[] occupants;
	
	public static void main(String[] args) throws JAXBException, IOException {
		Scanner in = new Scanner(System.in);
		String input;
		boolean read, create;
		do {
			System.out.print("Create or read file [C / R]:  ");
			input = in.nextLine().trim();
			read = "r".equalsIgnoreCase(input);
			create = "c".equalsIgnoreCase(input);
			if(!read && !create)
				System.out.println("Please enter a valid option.  ");
		} while(!read && !create);
		if(create) {
			ImageMap map = new ImageMap();
			boolean yes = false, no = false;
			do {
				System.out.print("Class name:  ");
				String className = in.next();
				do {
					System.out.print("State name:  ");
					String state = in.next();
					do {
						System.out.print("Action name:  ");
						String action = in.nextLine();
						do {
							System.out.print("Frame number:  ");
							int frame = in.nextInt();
							System.out.print("Image file path:  ");
							String path = in.next();
							
							map.put(className, state, action, new SpriteRecord(path, frame));
							do {
								System.out.print("Create another frame for this action [Y / n]:  ");
								input = in.next();
								yes = "y".equalsIgnoreCase(input);
								no = "n".equalsIgnoreCase(input);
								if(!yes && !no)
									System.out.println("Please enter a 'y' or an 'n' only.  ");
							} while (!yes && !no);
						} while (!no);
						do {
							System.out.print("Create another action for this state [Y / n]:  ");
							input = in.next();
							yes = "y".equalsIgnoreCase(input);
							no = "n".equalsIgnoreCase(input);
							if(!yes && !no)
								System.out.println("Please enter a 'y' or an 'n' only.  ");
						} while (!yes && !no);
					} while (!no);
					do {
						System.out.print("Create another state for this class [Y / n]:  ");
						input = in.next();
						yes = "y".equalsIgnoreCase(input);
						no = "n".equalsIgnoreCase(input);
						if(!yes && !no)
							System.out.println("Please enter a 'y' or an 'n' only.  ");
					} while (!yes && !no);
				} while (!no);
				do {
					System.out.print("Create another class [Y / n]:  ");
					input = in.next();
					yes = "y".equalsIgnoreCase(input);
					no = "n".equalsIgnoreCase(input);
					if(!yes && !no)
						System.out.println("Please enter a 'y' or an 'n' only.  ");
				} while (!yes && !no);
			} while(!no);
			System.out.println("Creating file...  ");
			File file = new File("imageData.cfg.xml");
			file.createNewFile();
			System.out.println("Creating XML context...");
			Marshaller m = JAXBContext.newInstance("luigibros.data.xml").createMarshaller();
			System.out.println("Writing data to file...");
			m.marshal(map, file);
			System.out.println("Done.  ");
		} else if(read) {
			Unmarshaller m = JAXBContext.newInstance("luigibros.data.xml").createUnmarshaller();
			Level level = (Level) m.unmarshal(new File("map.cfg.xml"));
			for(OccupentRecord record : level.occupants) {
				System.out.println(record.getClassName());
				System.out.println("\t" + record.getPosX() + " , " + record.getPosY());
			}
		}
	}
}
